package android.animationtext;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
/**
 * 帧动画
 * */
public class MainActivity extends AppCompatActivity {
    private ImageView imageView;


    private ImageView imageView1;


    /**
     * 动画的drawable，可以播放和停止动画
     */
    private AnimationDrawable drawable;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.imageView);
        imageView1 = (ImageView) findViewById(R.id.imageView1);

//        initAnim();

        initAnimcode();

    }


    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.btn://播放动画
                if (drawable != null) {

                    drawable.start();

                }
                break;
            case R.id.btn2://停止动画
                if (drawable != null) {

                    drawable.stop();
                }

                break;
            case R.id.btn3:
                Intent intent = new Intent(MainActivity.this,BetweenActivity.class);
                startActivity(intent);

                break;
        }
    }

    /**
     * 从布局文件中定义动画
     */
    private void initAnim() {

        if (imageView != null) {
            //通过imageView 获取到它所对应的drawable
            drawable = (AnimationDrawable) imageView.getDrawable();
        }

    }

    /**
     * 从代码中直接定义动画
     * */
        private void initAnimcode(){
            drawable = new AnimationDrawable();
           Resources resources = getResources();
            if (resources!=null){
                //给drawable添加帧；
            drawable.addFrame(resources.getDrawable(R.drawable.icon1),200);
            drawable.addFrame(resources.getDrawable(R.drawable.icon2),200);
            drawable.addFrame(resources.getDrawable(R.drawable.icon3),200);
            drawable.addFrame(resources.getDrawable(R.drawable.icon4),200);
            drawable.addFrame(resources.getDrawable(R.drawable.icon5),200);
            drawable.addFrame(resources.getDrawable(R.drawable.icon6),200);
            drawable.setOneShot(false);//setOneShot ：是否播放一次；true表示一次；fales循环播放；

            }
            if (imageView1!=null){
                imageView1.setImageDrawable(drawable);

            }

        }




}
